<template>
  <div class="chatroom-content">
    <div ref="chatList">
      <div class="item" v-for="item in list" :key="item.id">
        <div>
          <img :src="item.avatar">
          <span v-if="item.code===6">群聊</span>
          <span v-else>单聊</span>
        </div>
        <div>
          <div class="name-time">
            <span>{{ item.nickName }}</span>
            <span>{{formatDate(item.createTime)}}</span>
            <span></span>
          </div>
          <div class="msg">
            <div class="triangle"></div>
            <div>
              <msg-text v-if="item.type===0" :message="item.message"></msg-text>
              <msg-image v-else-if="item.type===1" :message="item.message"></msg-image>
              <msg-video v-else-if="item.type===2" :message="item.message"></msg-video>
              <msg-audio v-else-if="item.type===3" :message="item.message"></msg-audio>
            </div>
          </div>
        </div>
      </div>

    </div>
  </div>
</template>

<script setup lang="ts">

import {IChatMessage} from "@/api/types/chat";
import MsgAudio from "./components/msg-audio.vue";
import MsgText from "./components/msg-text.vue";
import MsgImage from "./components/msg-image.vue";
import MsgVideo from "./components/msg-video.vue";
import {ref, inject, Ref} from "vue";
import {formatDate} from "@/utils/constants";

const scroll = inject<Ref<Boolean>>('scrollFlag');
const chatList = ref<HTMLDivElement>()
const list = ref<Array<IChatMessage>>([])

const addMsg = (msg: IChatMessage) => {
  // console.log(msg)
  list.value.push(msg)

  // 是否自动滚动
  if (scroll!.value) {
    setTimeout(() => {
      chatList.value!.scrollTop = chatList.value!.scrollHeight
    }, 100)
  }
}

const clear = () => {
  list.value = []
}
defineExpose({addMsg,clear})


</script>

<style lang="scss" scoped>
$chat-bg: #e8e8e8;
div.chatroom-content {
  height: 100%;
  min-height: 500px;

  & > div {
    margin: 0 auto;
    width: 900px;
    height: 100%;
    background: #ffffff;
    overflow-y: auto;
  }

  @media screen and (max-width: 900px) {
    & > div {
      width: 700px;
    }
  }
  @media screen and (max-width: 700px) {
    & > div {
      width: 500px;
    }
  }

  div.item {
    min-height: 50px;
    display: flex;
    justify-content: flex-start;
    align-items: flex-start;
    margin: 10px;

    & > div:nth-child(1) {
      display: flex;
      flex-direction: column;

      & > img {
        width: 60px;
        height: 60px;
        vertical-align: middle;
        margin: 5px;
      }

      & > span {
        text-align: center;
        color: #595555;
        font-size: 13px;
      }
    }

    & > div:nth-child(2) {
      display: flex;
      flex-direction: column;
      max-width: 90%;
      align-items: flex-start;

      div.name-time {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 3px;
        width: 100%;

        & > span {
          text-align: center;
          color: #1c6ca1;
          font-size: 15px;
          margin-left: 20px;

          &:nth-child(2) {
            color: #b5b3b7;
            font-size: 10px;
          }
        }
      }

      div.msg {
        word-wrap: break-word;
        word-break: break-all;
        white-space: pre-wrap;
        background: $chat-bg;
        border-radius: 6px;
        margin-left: 10px;
        position: relative;
        min-height: 30px;
        font-size: 14px;
        padding: 5px;
        color: #2c2b2b;
        display: flex;
        justify-content: center;
        align-items: center;

        .triangle {
          position: absolute;
          left: -20px;
          top: 5px;
          width: 0px;
          height: 0px;
          border-width: 10px;
          border-style: solid;
          border-color: transparent $chat-bg transparent transparent;
        }
      }
    }

  }
}
</style>